import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/services/user_service.dart';

import '../../../../components/base_refresh/base_refresh_page_counter.dart';
import '../../../../http/api/api.dart';
import '../../../../model/video_base_model.dart';

class MineReleaseBrushVideoController extends GetxController
    with BaseRefreshPageCounter {
  EasyRefreshController refreshController = EasyRefreshController();

  final PagingController<int, VideoBaseModel> pagingControllers =
      PagingController(firstPageKey: 1);
  var videoStatus = 0; //1-待审核 2-已发布 3-未通过
  List<VideoBaseModel> videos = List<VideoBaseModel>.of([]);

  onRefresh(int videoStatus) async {
    this.videoStatus = videoStatus;
    resetPage();
    return await getReleaseBrushVideoList(isRefresh: true);
  }

  onLoad() async {
    incPage();
    return await getReleaseBrushVideoList();
  }

  getReleaseBrushVideoList({bool isRefresh = false}) async {
    try {
      if (isRefresh) {
        pagingControllers.refresh();
      }
      final response = await Api.getPublicationsVideosList(
        userId: Get.find<UserService>().user.userId ?? 0,
        page: page,
        pageSize: 20,
        videoMark: 2,
        videoStatus: videoStatus,
      );
      if (response!.isNotEmpty) {
        pagingControllers.appendPage(response, page);
        if (isRefresh == true) {
          _setList(response);
        } else {
          _addList(response);
        }
      } else {
        pagingControllers.appendLastPage([]);
      }
    } catch (_) {}
  }

  void _setList(List<VideoBaseModel>? list) {
    if (videoStatus > 1) return;
    videos.clear();
    _addList(list);
  }

  void _addList(List<VideoBaseModel>? list) {
    if (videoStatus > 1) return;
    if (null != list && list.isNotEmpty) {
      videos.addAll(list);
    }
  }

  onclickItem(int index) async {
    if (videoStatus == 2) {
      Get.toShortVideoPlay(videos, idx: index);
    } else if (videoStatus == 3) {
      List<int> ids = [];
      ids.add(videos[index].videoId!);
      final resp = await Api.delCheckVideo(videoId: ids);
      if (resp) EasyToast.show("删除成功");
      onRefresh(videoStatus);
    }
  }
}
